// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var {mySelect} = event
  mySelect.openid = openid;
  mySelect.addTime =Date.now();
  if(!mySelect.num)
  {
    mySelect.num=1
  }
  var res= await db.collection("xiangcun_cars").where({
    openid:openid,
    goodid:mySelect.goodid,
    // size:mySelect.size,
    // color:my Select.color
  }).get();
  if(!res.data.length){

    return await db.collection("xiangcun_cars").add({
        data:mySelect
      })
  }else{
    var oldData= res.data[0]
  return await db.collection("xiangcun_cars").doc(oldData._id).update({
      data:{
        num:(oldData.num+mySelect.num),
        // num:oldData+mySelect.num,
        addTime:Date.now()
      }
    })
  }

}